package com.yuanqi.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.Common.Pojo.Vo.PostVo;
import com.yuanqi.Common.Pojo.entity.User;
import com.yuanqi.Mapper.TopicMapper;
import com.yuanqi.Common.Pojo.Dto.TopicDto;
import com.yuanqi.Common.Pojo.Vo.PostDetail;
import com.yuanqi.Common.Pojo.Vo.TopicVo;
import com.yuanqi.Common.Pojo.entity.Topic;
import com.yuanqi.Common.Pojo.entity.TopicClass;
import com.yuanqi.Common.Pojo.entity.TopicPost;
import com.yuanqi.Service.PostService;
import com.yuanqi.Service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Slf4j
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Resource
    TopicMapper topicMapper;
    @Resource
    PostService postService;
    @Resource
    PostServiceImpl postServiceImpl;
    @Resource
    ThreadPoolExecutor threadPoolExecutor;
    @Override
    public List<TopicClass> getlist() {
        return topicMapper.getlist();
    }

    @Override
    public List<PostVo> getpostList(Integer topicId, Integer current,Boolean isTime,Long userId) throws InterruptedException {
        Integer start  = (current-1)*10;
       List<PostVo> postVos = topicMapper.getPostListByTopicId(topicId,start,isTime);
       postServiceImpl.extracted(postVos,0, userId);
       return postVos;
    }

    @Override
    public List<TopicVo> SearchTopic(TopicDto topicDto) throws ParseException, InterruptedException {
        LambdaUpdateWrapper<Topic> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(Topic::getTitle,topicDto.getTopicName());
        wrapper.eq(Topic::getType,1);
        Page<Topic> page = new Page<>(topicDto.getCurrent(),10);
        Page<Topic> page1 = topicMapper.selectPage(page, wrapper);
        List<Topic> records = page1.getRecords();
        if (records==null)
        {
            return null;
        }
        ArrayList<TopicVo> list1 = new ArrayList<>();
        Long startTime = getTodayStartTime();

        CountDownLatch latch = new CountDownLatch(records.size());
        records.forEach((item)->{
           threadPoolExecutor.execute(()->{
               TopicVo topicVo = BeanUtil.toBean(item, TopicVo.class);
               List<TopicPost> list = topicMapper.selectPostCount(item.getId());
               topicVo.setPostCount(list.size());
               Integer todayPostCount =0;
               for (TopicPost topicPost : list) {
                   if (topicPost.getCreateTime()==null)
                   {
                       continue;
                   }
                   if (topicPost.getCreateTime()>startTime)
                   {
                       todayPostCount++;
                   }
               }
               topicVo.setTodayPostCount(todayPostCount);
               list1.add(topicVo);
               latch.countDown();
           });
        });
        latch.await();
        return list1;
    }

//    获取热门话题，大于10才算热门话题
    @Override
    public List<TopicVo> getHotTopic(Integer current) throws ParseException {
        Integer start = (current-1)*10;
        Long time = getTodayStartTime()/1000;
        return topicMapper.selectHotTopicList(start,time);
    }

    /**
     * 根据话题分类获取话题
     * @param current
     * @param classId
     * @return
     * @throws ParseException
     * @throws InterruptedException
     */
    @Override
    public List<TopicVo> getTopicByClassId(Integer current, Integer classId) throws ParseException, InterruptedException {
        LambdaUpdateWrapper<Topic> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Topic::getTopicClassId,classId);
        wrapper.eq(Topic::getType,1);
        Page<Topic> page = new Page<>(current,10);
        Page<Topic> page1 = topicMapper.selectPage(page, wrapper);
        List<Topic> records = page1.getRecords();
        if (records==null)
        {
            return null;
        }
        ArrayList<TopicVo> list1 = new ArrayList<>();
        Long startTime = getTodayStartTime()/1000;
        CountDownLatch latch = new CountDownLatch(records.size());
        records.forEach((item)->{
            threadPoolExecutor.execute(()->{
                TopicVo topicVo = BeanUtil.toBean(item, TopicVo.class);
                List<TopicPost> list = topicMapper.selectPostCount(item.getId());
                topicVo.setPostCount(list.size());
                Integer todayPostCount =0;
                for (TopicPost topicPost : list) {
                    if (topicPost.getCreateTime()==null)
                    {
                        continue;
                    }
                    if (topicPost.getCreateTime()>startTime)
                    {
                        todayPostCount++;
                    }
                }
                topicVo.setTodayPostCount(todayPostCount);
                list1.add(topicVo);
                latch.countDown();
            });
        });
        latch.await();
        return list1;
    }


    private  Long getTodayStartTime() throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        String[] split = format.split(" ");
        format  = split[0]+" "+"00:00:00";
        Date parse = simpleDateFormat.parse(format);
       return parse.getTime();
    }
}
